/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GetMovimiento.java
 *
 * Created on 12/10/2011, 08:58:45 PM
 */
package scrumsprint1;

import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
/**
 *
 * @author rchic
 */
public class GetMovimiento1 extends javax.swing.JFrame {

    /** Creates new form GetMovimiento */
    public GetMovimiento1() {
        initComponents();

    }

    public GetMovimiento1( final Panel panel ){
        this.panel = panel;
        initComponents();
        try{
            setDefaultLookAndFeelDecorated(true);
            JDialog.setDefaultLookAndFeelDecorated( true );
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        }catch(Exception e){}
        SwingUtilities.updateComponentTreeUI( this );
        this.setLocationRelativeTo( null );
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        txtPosX = new javax.swing.JTextField();
        TxtPosY = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        comboDir = new java.awt.Choice();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jButton1.setText("Mover");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("posicion X:");

        jLabel2.setText("posicion Y:");

        jLabel3.setText("Direccion:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel2)
                        .addComponent(jLabel1))
                    .addComponent(jLabel3))
                .addGap(40, 40, 40)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(comboDir, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24)
                        .addComponent(jButton1))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(TxtPosY, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtPosX, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)))
                .addGap(41, 41, 41))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(txtPosX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(TxtPosY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addGap(8, 8, 8)))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jButton1))
                    .addComponent(comboDir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    String [] pathIma = new String[4];
    try{
    
    int posx=Integer.parseInt(this.txtPosX.getText());
    int posy=Integer.parseInt(this.TxtPosY.getText());
    String movimiento=this.comboDir.getSelectedItem();
    
    if(posx<0||posy<0||movimiento.equals("")){
        JOptionPane.showMessageDialog(this, "Error faltan datos", "Guerra", JOptionPane.ERROR_MESSAGE);
    }
    else{
        Movimiento1 posNave=new Movimiento1(posx,posy,movimiento);
        try{
            if(panel.tipoNave==1){
                int x=posNave.getPosx();
                int y = posNave.getPosy();
                if(panel.matrizCasillas[x-1][y-1].getIntElemento()==0){
                    pathIma[0]="/proyectoanalisis/Naves/btnNave1.jpg";
                    pathIma[1]="";
                    pathIma[2]="";
                    pathIma[3]="";
                    llenarDatosMatriz(x-1, y-1, 1, pathIma[0]);
                    panel.nav1=true;
                    this.setVisible(false);
                    panel.agregarVida( 1 );
                    panel.puedeAgregarNave();
                }
                else{
                    JOptionPane.showMessageDialog(this, "Error: Casilla ya ocupada, por favor elegir otra", "Guerra", JOptionPane.ERROR_MESSAGE);
                    txtPosX.setText("");
                    TxtPosY.setText("");
                }
            }
            else if(panel.tipoNave==2){
                if(posNave.getDireccion().equalsIgnoreCase("Horizontal")){
                    int x=posNave.getPosx()-1;
                    int y = posNave.getPosy()-1;
                    if(panel.matrizCasillas[x][y].getIntElemento()==0 &&panel.matrizCasillas[x+1][y].getIntElemento()==0){
                        pathIma[0]="/proyectoanalisis/Naves/btnNave2P1.jpg";
                        pathIma[1]="/proyectoanalisis/Naves/btnNave2P2.jpg";
                        pathIma[2]="";
                        pathIma[3]="";
                        llenarDatosMatriz(x, y, 1, pathIma[0]);
                        llenarDatosMatriz(x+1, y, 1, pathIma[1]);
                        panel.nav2=true;
                        this.setVisible(false);
                        panel.agregarVida( 2 );
                        panel.puedeAgregarNave();
                    }
                    else{
                        JOptionPane.showMessageDialog(this, "Error: Casilla ya ocupada, por favor elegir otra", "Guerra", JOptionPane.ERROR_MESSAGE);
                        txtPosX.setText("");
                        TxtPosY.setText("");
                    }
                }
                else if(posNave.getDireccion().equalsIgnoreCase("Vertical")){
                    int x=posNave.getPosx()-1;
                    int y = posNave.getPosy()-1;
                    if(panel.matrizCasillas[x][y].getIntElemento()==0 &&panel.matrizCasillas[x][y+1].getIntElemento()==0){
                        pathIma[0]="/proyectoanalisis/Naves/btnNave2P1.jpg";
                        pathIma[1]="/proyectoanalisis/Naves/btnNave2P2.jpg";
                        pathIma[2]="";
                        pathIma[3]="";
                        llenarDatosMatriz(x, y, 1, pathIma[0]);
                        llenarDatosMatriz(x, y+1, 1, pathIma[1]);
                        panel.nav2=true;
                        this.setVisible(false);
                        panel.agregarVida( 2 );  
                        panel.puedeAgregarNave();
                    }
                    else{
                        JOptionPane.showMessageDialog(this, "Error: Casilla ya ocupada, por favor elegir otra", "Guerra", JOptionPane.ERROR_MESSAGE);
                        txtPosX.setText("");
                        TxtPosY.setText("");
                    }
                }
            }
            else if(panel.tipoNave==3){
                if(posNave.getDireccion().equalsIgnoreCase("Horizontal")){
                    int x=posNave.getPosx()-1;
                    int y = posNave.getPosy()-1;
                    if(panel.matrizCasillas[x][y].getIntElemento()==0 &&panel.matrizCasillas[x+1][y].getIntElemento()==0&&panel.matrizCasillas[x+2][y].getIntElemento()==0){
                        pathIma[0]="/proyectoanalisis/Naves/btnNave3P1.jpg";
                        pathIma[1]="/proyectoanalisis/Naves/btnNave3P2.jpg";
                        pathIma[2]="/proyectoanalisis/Naves/btnNave3P3.jpg";
                        pathIma[3]="";
                        llenarDatosMatriz(x, y, 1, pathIma[0]);
                        llenarDatosMatriz(x+1, y, 1, pathIma[1]);
                        llenarDatosMatriz(x+2, y, 1, pathIma[2]);
                        panel.nav3=true;
                        this.setVisible(false);
                        panel.agregarVida( 3 );
                        panel.puedeAgregarNave();
                    }
                    else{
                        JOptionPane.showMessageDialog(this, "Error: Casilla ya ocupada, por favor elegir otra", "Guerra", JOptionPane.ERROR_MESSAGE);
                        txtPosX.setText("");
                        TxtPosY.setText("");
                    }
                }
                else if(posNave.getDireccion().equalsIgnoreCase("Vertical")){
                    int x=posNave.getPosx()-1;
                    int y = posNave.getPosy()-1;
                    if(panel.matrizCasillas[x][y].getIntElemento()==0 &&panel.matrizCasillas[x][y+1].getIntElemento()==0&&panel.matrizCasillas[x][y+2].getIntElemento()==0){
                        pathIma[0]="/proyectoanalisis/Naves/btnNave3P1.jpg";
                        pathIma[1]="/proyectoanalisis/Naves/btnNave3P2.jpg";
                        pathIma[2]="/proyectoanalisis/Naves/btnNave3P3.jpg";
                        pathIma[3]="";
                        llenarDatosMatriz(x, y, 1, pathIma[0]);
                        llenarDatosMatriz(x, y+1, 1, pathIma[1]);
                        llenarDatosMatriz(x, y+2, 1, pathIma[2]);
                        panel.nav3=true;
                        this.setVisible(false);
                        panel.agregarVida( 3 );
                        panel.puedeAgregarNave();
                    }
                    else{
                        JOptionPane.showMessageDialog(this, "Error: Casilla ya ocupada, por favor elegir otra", "Guerra", JOptionPane.ERROR_MESSAGE);
                        txtPosX.setText("");
                        TxtPosY.setText("");
                    }
                }
            }
            else if(panel.tipoNave==4){
                if(posNave.getDireccion().equalsIgnoreCase("Horizontal")){
                    int x=posNave.getPosx()-1;
                    int y = posNave.getPosy()-1;
                    if(panel.matrizCasillas[x][y].getIntElemento()==0 &&panel.matrizCasillas[x+1][y].getIntElemento()==0&&panel.matrizCasillas[x+2][y].getIntElemento()==0&&panel.matrizCasillas[x+3][y].getIntElemento()==0){
                        pathIma[0]="/proyectoanalisis/Naves/btnNave4P1.jpg";
                        pathIma[1]="/proyectoanalisis/Naves/btnNave4P2.jpg";
                        pathIma[2]="/proyectoanalisis/Naves/btnNave4P3.jpg";
                        pathIma[3]="/proyectoanalisis/Naves/btnNave4P4.jpg";
                        llenarDatosMatriz(x, y, 1, pathIma[0]);
                        llenarDatosMatriz(x+1, y, 1, pathIma[1]);
                        llenarDatosMatriz(x+2, y, 1, pathIma[2]);
                        llenarDatosMatriz(x+3, y, 1, pathIma[3]);
                        panel.nav4=true;
                        this.setVisible(false);
                        panel.agregarVida( 4 );
                        panel.puedeAgregarNave();
                    }
                    else{
                        JOptionPane.showMessageDialog(this, "Error: Casilla ya ocupada, por favor elegir otra", "Guerra", JOptionPane.ERROR_MESSAGE);
                        txtPosX.setText("");
                        TxtPosY.setText("");
                    }
                }
                else if(posNave.getDireccion().equalsIgnoreCase("Vertical")){
                    int x=posNave.getPosx()-1;
                    int y = posNave.getPosy()-1;
                    if(panel.matrizCasillas[x][y].getIntElemento()==0 &&panel.matrizCasillas[x][y+1].getIntElemento()==0&&panel.matrizCasillas[x][y+2].getIntElemento()==0&&panel.matrizCasillas[x][y+3].getIntElemento()==0){
                        pathIma[0]="/proyectoanalisis/Naves/btnNave4P1.jpg";
                        pathIma[1]="/proyectoanalisis/Naves/btnNave4P2.jpg";
                        pathIma[2]="/proyectoanalisis/Naves/btnNave4P3.jpg";
                        pathIma[3]="/proyectoanalisis/Naves/btnNave4P4.jpg";
                        llenarDatosMatriz(x, y, 1, pathIma[0]);
                        llenarDatosMatriz(x, y+1, 1, pathIma[1]);
                        llenarDatosMatriz(x, y+2, 1, pathIma[2]);
                        llenarDatosMatriz(x, y+3, 1, pathIma[3]);
                        panel.nav4=true;
                        this.setVisible(false);
                        panel.agregarVida( 4 );
                        panel.puedeAgregarNave();
                    }
                    else{
                        JOptionPane.showMessageDialog(this, "Error: Casilla ya ocupada, por favor elegir otra", "Guerra", JOptionPane.ERROR_MESSAGE);
                        txtPosX.setText("");
                        TxtPosY.setText("");
                    }
                }
            }
        }
        catch(Exception er){
            JOptionPane.showMessageDialog(this, "Error: No existe esa casilla en el tablero", "Guerra", JOptionPane.ERROR_MESSAGE);
            txtPosX.setText("");
            TxtPosY.setText("");
        }
    }

    
    
    }catch(Exception s){
        
    JOptionPane.showMessageDialog(this, "Error al ingresar los datos", "Guerra", JOptionPane.ERROR_MESSAGE);
     }
    
}//GEN-LAST:event_jButton1ActionPerformed

public void llenarDatosMatriz(int x, int y, int estado, String path){
    panel.btnMatrizBotones[x][y].setIcon(new javax.swing.ImageIcon(getClass().getResource(path)));
    panel.btnMatrizBotones[x][y].setEnabled(true);
    panel.matrizCasillas[x][y].setIntElemento(estado);
}
private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
    // TODO add your handling code here:
    GenerarDireccion();
}//GEN-LAST:event_formWindowActivated

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    // TODO add your handling code here:
    this.setVisible(false);
}//GEN-LAST:event_formWindowClosing


public void GenerarDireccion(){

this.comboDir.add("Horizontal");
this.comboDir.add("Vertical");

}
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GetMovimiento1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GetMovimiento1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GetMovimiento1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GetMovimiento1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GetMovimiento1().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField TxtPosY;
    private java.awt.Choice comboDir;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtPosX;
    // End of variables declaration//GEN-END:variables
    private Panel panel;
}
